package com.douwong.student.fragment;

import android.annotation.SuppressLint;

import com.douwong.student.activity.ClassTestActivity;
import com.douwong.student.application.BaseApplication;
import com.douwong.student.base.BaseChoiceFragment;
import com.douwong.student.interfaces.ClassTestInterface;
import com.douwong.student.model.ResTestProblemsInfo;
import com.douwong.student.model.TestInfos;

/**
 * @autor yichao
 * @time 2017/4/516:05
 * @des ${TODO}
 */
@SuppressLint("ValidFragment")
public class MultiChoiceFragment extends BaseChoiceFragment implements ClassTestInterface {
    private String chooseLastAnswer="";
    public MultiChoiceFragment(ClassTestActivity classTestActivity, ResTestProblemsInfo.TopicItemBean topicItemBean) {
        super(classTestActivity, topicItemBean);
    }




    @Override
    protected void setSlectViewState(String str) {
        for (int i = 0; i < linearlayouts.length; i++) {
            if (str.equals(chart[i])) {
                if (mContenView.findViewById(linearlayouts[i]).isSelected()) {
                    mContenView.findViewById(linearlayouts[i]).setSelected(false);
                    chooseLastAnswer= chooseLastAnswer.replace(chart[i], "");
                } else {
                    mContenView.findViewById(linearlayouts[i]).setSelected(true);
                    chooseLastAnswer= chooseLastAnswer + chart[i];
                }

            }
        }

    }

    @Override
    public TestInfos getAnswerInfo() {
        if (chooseLastAnswer.equals("")){
            BaseApplication.noDoTestSum++;
            return new TestInfos(mTopicItemBean.getId(),0,new String[]{chooseLastAnswer});
        }else {
            double sore=0;
            char[] chars = chooseLastAnswer.toCharArray();

            if (chars.length==mTopicItemBean.getAnswers().size()){
                for (int i = 0; i < chars.length; i++) {

                    boolean contains = mTopicItemBean.getAnswers().contains(String.valueOf(chars[i]));
                    if (!contains){
                        sore=0;
                        break;
                    }else {
                        sore=mTopicItemBean.getScore();
                    }
                }
            }

            String [] optionStrArr=new String[chars.length];
            for (int i = 0; i < chars.length; i++) {
                optionStrArr[i]= String.valueOf(chars[i]);
            }
            return new TestInfos(mTopicItemBean.getId(),sore,optionStrArr);
        }
    }

    @Override
    public void setCommitStatue() {
        for (int i = 0; i < linearlayouts.length; i++) {
            mContenView.findViewById(linearlayouts[i]).setClickable(false);
        }
    }
}
